package com.talent.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.talent.page.Page;
import com.talent.pojo.Depart;
import com.talent.pojo.Major;
import com.talent.pojo.User;
import com.talent.service.DepartService;
import com.talent.service.MajorService;
import com.talent.service.OpenStatusService;
import com.talent.service.ScheduleService;
import com.talent.service.TrainDetailService;
import com.talent.service.TrainPlanService;
import com.talent.vo.Result;
import com.talent.vo.ScheduleVo;
import com.talent.vo.TrainDetailVo;
import com.talent.vo.TrainPlanVo;

@Controller
@RequestMapping("/schedule")
public class ScheduleController extends BaseController{
	@Resource
	private TrainPlanService trainPlanService;
	@Resource
	private TrainDetailService trainDetailService;
	@Resource
	private ScheduleService scheduleService;
	@Resource
	private DepartService departService;
	@Resource
	private MajorService majorService;
	@Resource
	private OpenStatusService openStatusService;
	 @RequestMapping(value = "/manager",method = RequestMethod.GET)
	 public ModelAndView scheduleUI(TrainDetailVo trainDetail,Integer enterterm,Integer currentPage, ModelAndView mav){
	        try {
	        	if(trainDetail.getTrainPlanVo()!=null){
	        		if(trainDetail.getTrainPlanVo().getEnterterm()== null){
	        			trainDetail.getTrainPlanVo().setEnterterm(enterterm);
	        		}
	        	}
	        	User user = getCurrentUser(getSession());
	            Page<TrainDetailVo> page = trainDetailService.selectTrainDetailPage(trainDetail,user, currentPage == null ? 1 : currentPage);
	            List<Depart> departList = departService.selectAllDepart();
	            List<Major> majorList = majorService.selectAllMajor();
	            // 列表项
	            mav.addObject("page", page.getResultList());
	            mav.addObject("enterterm", enterterm);
	            mav.addObject("departList", departList);
	            mav.addObject("majorList", majorList);
	            // 分页参数
	            initPageParams(mav, page);
	            // 搜索条件
	            mav.addObject("trainDetail", trainDetail);
	            mav.addObject("trainDetailList",page.getResultList());
	            mav.setViewName("schedule/manager");
	        }catch (Exception e){
	            mav.setViewName("error/500");
	            e.printStackTrace();
	        }
	        return mav;
	 }
	 
	 @RequestMapping("detail")
	  	public ModelAndView detail(Integer id) {
	  		ModelAndView mav = new ModelAndView();
	  		try {
	  			TrainPlanVo trainPlan = trainPlanService.selectVoByPrimaryKey(id);
	  			List<TrainDetailVo> trainDetailList =  trainDetailService.selectVoByTrainPlanKey(id);
	  			int tpId = 2;
	        	int status = openStatusService.selectStatusById(tpId);
	        	 mav.addObject("opstatus", status);
	  			mav.addObject("trainPlan", trainPlan);
	  			mav.addObject("trainDetailList", trainDetailList);
	  			mav.setViewName("schedule/trainPlanDetail");
	  		} catch (Exception e) {
	  			e.printStackTrace();
	  			mav.setViewName("common/error");
	  			mav.addObject("error", "有异常！");
	  		}
	  		return mav;
	  	}
	 @RequestMapping("detail1")
	 public ModelAndView detail1(Integer id) {
		 ModelAndView mav = new ModelAndView();
		 try {
			 TrainDetailVo trainDetail =  trainDetailService.selectVoByPrimaryKey(id);
			 int tpId = 2;
        	 int status = openStatusService.selectStatusById(tpId);
        	 mav.addObject("opstatus", status);
			 ScheduleVo scheduleVo =  scheduleService.selectVoByTrainDetailId(id);
			 if(scheduleVo != null){
				 trainDetail.setScheduleVo(scheduleVo);
			 }
			 mav.addObject("trainDetail", trainDetail);
			 mav.setViewName("schedule/scheduleDetail");
		 } catch (Exception e) {
			 e.printStackTrace();
			 mav.setViewName("common/error");
			 mav.addObject("error", "有异常！");
		 }
		 return mav;
	 }
	 
	 @RequestMapping("trainPlanCheckYes")
	   	@ResponseBody
	   	public Result trainPlanCheckYes(Integer id) {
	   		try {
	   			int yes = 1;
	   			scheduleService.updateStatus(id,yes);
	   			return new Result(true);
	   		} catch (Exception e) {
	   			e.printStackTrace();
	   			return new Result(false);
	   		}
	   	}
	    
	    @RequestMapping("trainPlanCheckNoUI")
		public ModelAndView trainPlanCheckNoUI(Integer id) {
			ModelAndView mav = new ModelAndView();
			try {
				mav.addObject("id", id);
				mav.setViewName("schedule/checkNo");
			} catch (Exception e) {
				e.printStackTrace();
				mav.setViewName("common/error");
				mav.addObject("error", "有异常！");
			}
			return mav;
		}

		@RequestMapping("trainPlanCheckNo")
		@ResponseBody
		public Result trainPlanCheckNo(int id,String suggestion) {
			try {
				int no = 2;
				scheduleService.updateStatus(id,no);
				scheduleService.updateSuggestion(id, suggestion);
				return new Result(true);
			} catch (Exception e) {
				e.printStackTrace();
				return new Result(false);
			}
		}
		
	 @RequestMapping("trainPlanCheckNoReason")
	    public ModelAndView trainPlanCheckNoUI(String suggestion) {
	    	ModelAndView mav = new ModelAndView();
	    	try {
	    		mav.addObject("suggestion", suggestion);
	    		mav.setViewName("schedule/checkNoReason");
	    	} catch (Exception e) {
	    		e.printStackTrace();
	    		mav.setViewName("common/error");
	    		mav.addObject("error", "有异常！");
	    	}
	    	return mav;
	    }
	 @RequestMapping("trainPlanUpdateUI")
		public ModelAndView trainPlanUpdateUI(Integer id) {
			ModelAndView mav = new ModelAndView();
			try {
				ScheduleVo scheduleVo = scheduleService.selectVoByPrimaryKey1(id);
	  			mav.addObject("scheduleVo", scheduleVo);
				mav.setViewName("schedule/trainPlanUpdate");
			} catch (Exception e) {
				e.printStackTrace();
				mav.setViewName("common/error");
				mav.addObject("error", "有异常！");
			}
			return mav;
		}

		@RequestMapping("trainPlanUpdate")
		@ResponseBody
		public Result trainPlanUpdate(Integer scheduleId,Integer theorystartweek,Integer theoryendweek,Integer expstartweek,Integer expendweek,String theoryteacher,String expteacher) {
			try {
				scheduleService.edit(scheduleId,theorystartweek,theoryendweek,expstartweek,expendweek,theoryteacher,expteacher);
				return new Result(true);
			} catch (Exception e) {
				e.printStackTrace();
				return new Result(false);
			}
		}
}
